import requests

from lxml import etree
if __name__ == "__main__":
    #1.指定url

    #2.进行UA伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'

    }
    #3.post请求参数处理（同get请求一致）
    word = input('enter a word:')
    param = {
        'query': '十字门华发商都',
        'region': '珠海',
        'output': 'xml',
        'ak': 'uwdrLZeQgWir9SnDLQ4GzWyL3tpAU4vF'
    }
    post_url = 'http://api.map.baidu.com/place/v2/search?query='+word+'&region=珠海&output=json&ak=uwdrLZeQgWir9SnDLQ4GzWyL3tpAU4vF'
    response = requests.get(url=post_url, param=param, headers=headers)
    # 5.获取响应数据:json()方法返回的是obj（如果确认响应数据是json类型的，才可以使用json（））
    # 持久化存储
    tree = etree.XML(response)
    li_list = tree.xpath('//results[1]/location/lat')
    print(li_list)